package com.base.system.managedBean;

import java.io.Serializable;

import javax.faces.context.FacesContext;

import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.layout.UICell;
import org.operamasks.faces.user.util.Browser;

import com.base.commonKey.entity.CommonKey;
import com.base.commonKey.entity.CommonKeyType;
import com.base.system.managedBean.baseBean.BaseCommonKeyBean;

@ManagedBean(name = "system_addCommonKeyForKnownTypeBean", scope = ManagedBeanScope.REQUEST)
public class AddCommonKeyForKnownTypeBean extends BaseCommonKeyBean implements
		Serializable {

	@Accessible
	private boolean parentRendered = true;

	@SaveState
	private String keyTypeForKnow;

	@BeforeRender
	public void beforeRender(boolean isPostBack) {
		if (!isPostBack) {
			keyType = FacesContext.getCurrentInstance().getExternalContext()
					.getRequestParameterMap().get("keyType");
			keyTypeForKnow = keyType;
			CommonKeyType ckt = commonKeyService
					.getCommonKeyTypeByTypeCode(keyType);
			if (ckt.getIsLay() == true) {
				parentRendered = true;
			} else {
				parentRendered = false;
			}
		}
	}

	@Action(id = "saveCommonKey")
	public void saveCommonKey() {
		commonKey = new CommonKey();
		setCommonKeyProperty();
		commonKey.setKeyType(keyTypeForKnow);
		commonKeyDao.insert(commonKey);
		Browser
				.execClientScript("window.parent.gridReloadJsvar.fireEvent('click');");
	}

	@Action(id = "cancel")
	public void cancel() {
		Browser
				.execClientScript("window.parent.addCloseJsvar.fireEvent('click');");
	}

	@Bind(id = "parentIdTitle")
	private UICell parentIdTitle;

	@Bind(id = "parentIdContent")
	private UICell parentIdContent;
}
